package com.topboya.linker.ui.activity;

import android.content.Intent;
import android.preference.PreferenceManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.topboya.linker.R;
import com.topboya.linker.diyview.SelectCustomerDialog;
import com.topboya.linker.model.response.CustomerByOrgIdResponse;
import com.topboya.linker.model.response.FollowUserByOfficeResponse;
import com.topboya.linker.model.response.FollowUserLateResponse;
import com.topboya.linker.model.response.OrganizationResopnse;
import com.topboya.linker.model.response.ProductInforListReponse;
import com.topboya.linker.model.response.SelectFileResponse;
import com.topboya.linker.model.response.VisitTypeResponse;
import com.topboya.linker.model.save.VisitCustomerBean;
import com.topboya.linker.ui.adapter.CreateTask1Adapter;
import com.topboya.linker.ui.adapter.HeaderAndFooterRecyclerViewAdapter;
import com.topboya.linker.ui.adapter.SelectProductAdapter;
import com.topboya.linker.ui.base.Presenter.BasePresenter;
import com.topboya.linker.ui.base.activity.BaseActivity;
import com.topboya.linker.ui.presenter.CreateTaskPrensenter;
import com.topboya.linker.ui.view.CreateTaskView;
import com.topboya.linker.utils.LogUtils;
import com.topboya.linker.utils.UIUtils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class CreateTaskActivity1 extends BaseActivity<CreateTaskView,CreateTaskPrensenter> implements CreateTaskView {
    private static final String TAG = "CreateTaskActivity1:";
    private TextView toolbar_title;
    private TextView tv_device_name;
    private LinearLayout ll_create_task;

    private OrganizationResopnse.DocDetailInfoList docDetailInfo;
    private ArrayList<String> selectepartnameList;
    private  LinearLayout.LayoutParams layoutParams;
    private List<CustomerByOrgIdResponse.DepartmentList> departmentList;
    private List<String> alldepartmentList;
    private Button btn_save;
    private List<VisitCustomerBean> visitCustomerBeans ;
    private RecyclerView rl_user_container;
//    返回选择的学术类型
    private ArrayList<VisitTypeResponse.Data> visittypelist;
//
    private ArrayList<ProductInforListReponse.ProductDetailInfoList> productDetailInfoLists;

    private  ArrayList<ArrayList<SelectFileResponse.FileList>> fileListss;

    private ArrayList<FollowUserLateResponse.LateUser> selectsameDepartLists;

    private CreateTaskPrensenter createTaskPrensenter;
    @Override
    protected int getLayoutId() {

        return R.layout.activity_create_task1;
    }

    @Override
    protected void initView() {

        toolbar_title = findViewById(R.id.toolbar_title);
        tv_device_name = findViewById(R.id.tv_device_name);
        ll_create_task = findViewById(R.id.ll_create_task);
        btn_save = findViewById(R.id.btn_save);
        rl_user_container = findViewById(R.id.rl_user_container);
        initTitle();
    }

    private void initTitle() {

        toolbar_title.setText("创建任务");
    }
    @Override
    protected void initData() {

        initdocDetailInfo();
//      每次进来初始化重置view下标

        departmentList = (List<CustomerByOrgIdResponse.DepartmentList>)getIntent().getSerializableExtra("departmentList");
        selectepartnameList = getIntent().getStringArrayListExtra("selectepartnameList");

        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this,
                LinearLayoutManager.VERTICAL, false);
//        item下面横向目前是在条目布局加了一个view，后面改为代码设置
        View view = View.inflate(this,R.layout.create_task_footer_view,null);
        layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(0, 50, 0, 0);
        view.setLayoutParams(layoutParams);
        CreateTask1Adapter createTask1Adapter = new CreateTask1Adapter(this,createTaskPrensenter,selectepartnameList,departmentList ,view,docDetailInfo);
        HeaderAndFooterRecyclerViewAdapter  headerAndFooterRecyclerViewAdapter = new HeaderAndFooterRecyclerViewAdapter(createTask1Adapter);
        headerAndFooterRecyclerViewAdapter.addFooterView(view);
        rl_user_container.setLayoutManager(layoutManager);
        rl_user_container.setAdapter(headerAndFooterRecyclerViewAdapter);

    }

    private void initdocDetailInfo() {

        docDetailInfo = (OrganizationResopnse.DocDetailInfoList) getIntent().getSerializableExtra("DocDetailInfo");
        String devicesname = "<font color=\"#AAAAAA\">"+"终端名称："+"</font>"+"<font color=\"#000000\">"+docDetailInfo.getOrgName()+"</font>";
        tv_device_name.setText(Html.fromHtml(devicesname));

    }



    private void addView(LinearLayout.LayoutParams layoutParams, int i,boolean isfirst) {

        View view = View.inflate(this,R.layout.create_task_item,null);
        view.setLayoutParams(layoutParams);
        VisitCustomerBean visitCustomerBean =new VisitCustomerBean();
        TextView tv_sustomer_name = view.findViewById(R.id.tv_sustomer_name);
        TextView tv_delete = view.findViewById(R.id.tv_delete);
        View viewline = view.findViewById(R.id.view_line);

            tv_delete.setVisibility(View.GONE);
            viewline.setVisibility(View.GONE);

        if (isfirst){
            //第一次进来就直接添加选中的客户
            tv_sustomer_name.setText(selectepartnameList.get(i));
//            visitCustomerBean.setName(selectepartnameList.get(i));
            visitCustomerBeans.add(visitCustomerBean);
            }else {

            tv_sustomer_name.setText("请选择客户姓名");
        }

        tv_delete.setOnClickListener(v1 -> {

            ll_create_task.removeView(view);

        });

        ll_create_task.addView(view);
    }

    private void showSelectDialog(ArrayList<String> tempdepartmentList) {

        final SelectCustomerDialog selectCustomerDialog = SelectCustomerDialog.newInstance(tempdepartmentList);
        selectCustomerDialog.show(getFragmentManager(), "select");
        selectCustomerDialog.setCancelable(false);
        selectCustomerDialog.setOnSelectListener(selectPosSet -> {

            Iterator<Integer> value = selectPosSet.iterator();
            while (value.hasNext()) {

                int s = value.next();
                LogUtils.v(TAG + "s:"+s);

            }
            selectCustomerDialog.dismiss();

        });

    }

    @Override
    protected void initListener() {

    }

    @Override
    protected CreateTaskPrensenter createPresenter() {
        createTaskPrensenter = new CreateTaskPrensenter(this);
        return createTaskPrensenter;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
//        1001表示返回的是学术类型
         if (resultCode ==1001){
             visittypelist = new ArrayList<>();
             visittypelist.clear();
             visittypelist = (ArrayList<VisitTypeResponse.Data>) data.getSerializableExtra("result");
             LogUtils.v(TAG + "result:"+visittypelist.toString());
             onActivityResultListenr.onActivityResult(visittypelist,productDetailInfoLists,fileListss);
         }
//         1002表示返回的是产品信息和资料信息
         if (resultCode ==1002){

             productDetailInfoLists = (ArrayList<ProductInforListReponse.ProductDetailInfoList>)data.getSerializableExtra("productDetailInfoList");
             LogUtils.v(TAG + "result:"+productDetailInfoLists.toString());

             fileListss = (ArrayList<ArrayList<SelectFileResponse.FileList>>)data.getSerializableExtra("fileLists");
             LogUtils.v(TAG + "result:"+fileListss.toString());
             onActivityResultListenr.onActivityResult(visittypelist,productDetailInfoLists,fileListss);
         }

        //表示的是返回随访人
        if (resultCode ==1003){

            selectsameDepartLists = (ArrayList<FollowUserLateResponse.LateUser>) data.getSerializableExtra("selectlateUsersList");

            LogUtils.v(TAG + "selectsameDepartLists:"+selectsameDepartLists.toString());

            onActivityResultFollowUserListenr.onActivityResult(selectsameDepartLists);

        }


    }
   public onActivityResultListenr onActivityResultListenr;

    public void setonActivityResultListenr(onActivityResultListenr onActivityResultListenr){

        this.onActivityResultListenr = onActivityResultListenr;
    }

    @Override
    public void createTaskSuccess() {


        Toast.makeText(this,"创建任务成功",Toast.LENGTH_LONG).show();
        jumpToActivity(MainActivity.class);
        finish();

    }

    @Override
    public void getVisitType(List<VisitTypeResponse.Data> visitTypeResponses) {

    }

    public interface  onActivityResultListenr{

         void onActivityResult(ArrayList<VisitTypeResponse.Data> visittypelist ,ArrayList<ProductInforListReponse.ProductDetailInfoList> productDetailInfoLists,ArrayList<ArrayList<SelectFileResponse.FileList>> fileListss);
    }

    //随访人的回调
    public CreateTaskActivity1.onActivityResultFollowUserListenr onActivityResultFollowUserListenr;

    public void setonActivityResultFollowUserListenr(CreateTaskActivity1.onActivityResultFollowUserListenr onActivityResultFollowUserListenr){

        this.onActivityResultFollowUserListenr = onActivityResultFollowUserListenr;
    }
    public interface  onActivityResultFollowUserListenr{

        void onActivityResult(ArrayList<FollowUserLateResponse.LateUser> selectsameDepartLists);
    }
}
